//
//  HEOrderLeaveMsgCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/27.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEOrderLeaveMsgCell.h"
#import "UITableView+FDIndexPathHeightCache.h"

@interface HEOrderLeaveMsgCell ()<QMUITextViewDelegate>

@property(nonatomic, strong) QMUITextView *textView;
@property (nonatomic,strong) MASConstraint *heightconstrain;
@property(nonatomic, strong) UILabel *titleLb;
@property(nonatomic, assign) BOOL showSeparator;



@end

@implementation HEOrderLeaveMsgCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
         self.selectionStyle = UITableViewCellSelectionStyleNone;

         _showSeparator = YES;
         [self initUI];
     }
     return self;;
 }

- (void)initUI {
    
    _titleLb = [UILabel new];
    _titleLb.font = DYBlodFont(16);
    _titleLb.text = @"留言";
    _titleLb.textColor = CNavLightBlackColor;
    _titleLb.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_titleLb];
    
     [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(self.contentView).mas_offset(20);
         make.left.equalTo(self.contentView).mas_offset(15);

//         make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-15);
     }];
    
    _textView = [QMUITextView  new];
    // 限制可输入的字符长度
    self.textView.maximumTextLength = 500;
    self.textView.placeholder = @"请填写备注信息，最多输入500个字!";
    self.textView.placeholderColor = UIColorPlaceholder; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: UIFontMake(15),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.backgroundColor = CSplitLineColor;
    [self.contentView addSubview:_textView];
    self.textView.layer.cornerRadius = 4;
    [_textView setDelegate:self];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(_titleLb.mas_right).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.height.mas_greaterThanOrEqualTo(70);
        make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-15);
    }];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {


    self.indexPath = indexPath;
    _textView.text = formModel.order_leaveMsg;
}

#pragma mark - <QMUITextViewDelegate>
// 实现这个 delegate 方法就可以实现自增高
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
    
}

- (void)textView:(QMUITextView *)textView didPreventTextChangeInRange:(NSRange)range replacementText:(NSString *)replacementText {
    [Dialog toast:[NSString stringWithFormat:@"文字不能超过 %@ 个字符", @(textView.maximumTextLength)] delay:2.0];
}
 
- (void)textViewDidChange:(UITextView *)textView {
 
    self.formModel.order_leaveMsg = textView.text;
    
    
//    CGRect bounds = textView.bounds;
//    CGFloat oldHeight = bounds.size.height;
//    CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX);
//    CGSize newSize = [textView sizeThatFits:maxSize];
//    CGFloat newHeight = newSize.height;
//    bounds.size = newSize;
//    textView.bounds = bounds;
//    if (oldHeight != newHeight) {
//        UITableView *tableView = [self tableView];
//        [tableView.fd_indexPathHeightCache invalidateAllHeightCache];
//
////        [tableView.fd_indexPathHeightCache invalidateHeightAtIndexPath:_indexPath];
//        [self tableView:tableView willDisplayCell:self forRowAtIndexPath:_indexPath];
//
//        [tableView beginUpdates];
//        [tableView endUpdates];
//
//        if ( self.block) {
//            self.block(newHeight);
//        }
//    }
 
}


//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    CGFloat cornerRadius = 8.f;
//    cell.backgroundColor = UIColor.clearColor;
//
//    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
//    CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init];
//    CGMutablePathRef pathRef = CGPathCreateMutable();
//    CGRect bounds = cell.bounds;
//
//    NSInteger numberOfRows = 0;
//
//    numberOfRows = 1;
////    if (self.numberOfRowsInSection) {
////        numberOfRows = self.numberOfRowsInSection(self,self.tag -100);
////    }
//
//    BOOL needSeparator = NO;
//
//    if (indexPath.row == 0 && numberOfRows == 1) {
//        CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
//    }else if (indexPath.row == 0) {
//        // 初始起点为cell的左下角坐标
//        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
//        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
//        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
//        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
//
//        needSeparator = YES;
//
//    } else if (indexPath.row == numberOfRows -1) {
//        // 初始起点为cell的左上角坐标
//        CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
//        CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
//        CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
//        CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
//    } else {
//        CGPathAddRect(pathRef, nil, bounds);
//        needSeparator = YES;
//    }
//
//    layer.path = pathRef;
//    backgroundLayer.path = pathRef;
//    CFRelease(pathRef);
//    layer.fillColor = [UIColor whiteColor].CGColor;
//
//    if (self.showSeparator && needSeparator) {
//        CALayer *lineLayer = [[CALayer alloc] init];
//        CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
//        lineLayer.frame = CGRectMake(self.separatorInset.left, bounds.size.height - lineHeight, bounds.size.width - (self.separatorInset.left + self.separatorInset.right), lineHeight);
//        lineLayer.backgroundColor = CSplitLineColor.CGColor;
//        [layer addSublayer:lineLayer];
//    }
//
//    UIView *roundView = [[UIView alloc] initWithFrame:bounds];
//    [roundView.layer insertSublayer:layer atIndex:0];
//    roundView.backgroundColor = UIColor.clearColor;
//    cell.backgroundView = roundView;
//
//    UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
//    backgroundLayer.fillColor = [UIColor systemGroupedBackgroundColor].CGColor;
//    [selectedBackgroundView.layer insertSublayer:backgroundLayer below:cell.layer];
//    selectedBackgroundView.backgroundColor = UIColor.clearColor;
//    cell.selectedBackgroundView = selectedBackgroundView;
//}


// 寻找Cell所属的tableView
- (UITableView *)tableView {
    UIView *tableView = self.superview;
    while (![tableView isKindOfClass:[UITableView class]] && tableView) {
        tableView = tableView.superview;
    }
    return (UITableView *)tableView;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
